/**
 * @author Ludovic
 * 
**/

// var prev_id = getURLParam("prev_id");
// prev_id is passed as param adh! 
var prev_id = getURLParam("adh");

function switchRoutage(){
    // First we ensure we have a prev_id. If not get back.
    
    if (prev_id.length > 0 && prev_id != null ){
        var chkRoutage = document.getElementById('chkROUTAGE');
        if (chkRoutage.checked==true){
            switchRoutage_Set(prev_id);
        }
        else {
            switchRoutage_Reset(prev_id);
        }
    }
}

function switchRoutage_Set(prev_id){
    // Make asynch call to switch routage
    asynchHandler.init();
    asynchHandler.setEventSink(switchRoutage_Set_CallBack);
    var url = 'RoutageSetAdhByPrevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function switchRoutage_Set_CallBack(){
    var result = asynchHandler.receivedData;
    if (result != "SUCCESS"){
        alert("Attention, la sauvegarde du routage n'a pas pu etre faite...");
        var chkRoutage = document.getElementById('chkROUTAGE');
        chkRoutage.checked=false;
    }
}

function switchRoutage_Reset(prev_id){
    // Make asynch call to switch routage
    asynchHandler.setEventSink(switchRoutage_Reset_CallBack); //Do we need to handle the call back? Yes, for errors.
    var url = 'RoutageResetAdhByPrevId.aspx?prev_id=' + prev_id;
    asynchHandler.sendHttpRequest('GET', url );
}

function switchRoutage_Reset_CallBack(){
    var result = asynchHandler.receivedData;
    if (result != "SUCCESS"){
        alert("Attention, la sauvegarde du routage n'a pas pu etre faite...");
        var chkRoutage = document.getElementById('chkROUTAGE');
        chkRoutage.checked=true;
    }
}

function loadRoutage(){
    if (prev_id.length > 0 && prev_id != null) {
		asynchHandler.init();
		asynchHandler.setEventSink(loadRoutage_CallBack);
		var url = 'RoutageGetAdhByPrevId.aspx?prev_id=' + prev_id;
		asynchHandler.sendHttpRequest('GET', url);
	}
}

function loadRoutage_CallBack(){
    var result = asynchHandler.receivedData;

    var chkRoutage = document.getElementById('chkROUTAGE');
    if (result == "0" ) {
        chkRoutage.checked=false;
    }
    else if (result = "1") {
        chkRoutage.checked = true;
    }   
}
